package cas;

import java.util.Date;

public class Speed {
	private double currentSpeed; //mps
	private final double ACCELERATION = 0.8f;
	private final double DECELERATION = -0.8f;
	private double destinationSpeed;
	private boolean accelerating;
	private long time;
	private double timeDiff;
	
	public Speed(double currentSpeed, double destinationSpeed) {
		super();
		time = 0;
		this.currentSpeed = currentSpeed;
		this.destinationSpeed = destinationSpeed;
		if(currentSpeed<destinationSpeed){
			accelerating = false;
		}else{
			accelerating = true;
		}
	}
	public void calculateSpeed(){
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if((isAccelerating()&&currentSpeed>=destinationSpeed)||
				(!isAccelerating()&&currentSpeed<=destinationSpeed)){
			currentSpeed = destinationSpeed;
			time = new Date().getTime();
		}else{
			long currentTime = new Date().getTime();
			timeDiff =((Double.valueOf(currentTime)-Double.valueOf(time))/1000.0);
			currentSpeed = currentSpeed + (isAccelerating()?ACCELERATION:DECELERATION)*((Double.valueOf(currentTime)-Double.valueOf(time))/1000.0);
			time = currentTime;
		}
	}
	public double getCurrentSpeed() {
		return currentSpeed;
	}
	public void setCurrentSpeed(double currentSpeed) {
		this.currentSpeed = currentSpeed;
	}
	
	public double getDestinationSpeed() {
		return destinationSpeed;
	}
	public void setDestinationSpeed(double destinationSpeed) {
		this.destinationSpeed = destinationSpeed;
		if(currentSpeed<destinationSpeed){
			accelerating = true;
		}else{
			accelerating = false;
		}
	}
	public boolean isAccelerating() {
		return accelerating;
	}
	public void setAccelerating(boolean accelerating) {
		this.accelerating = accelerating;
	}
	public double getTimeDiff(){
		return timeDiff;
	}
}
